<?php
class json {
	public function jsons($array) {
		$this->objectToArray ( $array );
		$this->arrayRecursive ( $array, true );
		
		$json = json_encode ( $array );
		return urldecode ( $json );
	}
	
	public function arrayRecursive(&$array, $apply_to_keys_also = false)
	{
		
		foreach ($array as $key => $value) {
			 if (is_array($value)) {
				$this->arrayRecursive($array[$key], $apply_to_keys_also);
			} else {
				if(is_string($value)){
 					$encode = mb_detect_encoding($value, array("ASCII","GB2312","GBK","UTF-8"));
 					//if($encode != "UTF-8"){
 					//	$value = iconv($encode, "UTF-8", $value);
 					//}
					$value = $this->delete($value);
 				}
				
				$array[$key] = urlencode($value);
				//$array[$key] = $value;
			}
	
			if ($apply_to_keys_also && is_string($key)) {
				$new_key = urlencode($key);
				if ($new_key != $key) {
					$array[$new_key] = $array[$key];
					unset($array[$key]);
				}
			}
		}
	}
	
	public function delete($str) {
		$str = trim($str);
		$str = ereg_replace("\t","",$str);
		$str = ereg_replace("\r\n","",$str);
		$str = ereg_replace("\r","",$str);
		$str = ereg_replace("\n","",$str);
		$str = ereg_replace("    ","",$str);
		return trim($str);
	}
	public function objectToArray(&$array) {
		foreach ( $array as $key => $value ) {
			if (is_object ( $value )) {
				$array [$key] = get_object_vars ( $value );												
			}
			
			if (is_array ( $array[$key] )) {
				$this->objectToArray($array[$key]);
			}
			
		}
	}
	
	public function uuid() {
		$charid = md5 ( uniqid ( rand (), true ) );
		$hyphen = chr ( 45 ); // "-"
		$uuid = 		// chr(123)// "{".
		substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 )
		//.chr(125)// "}"
		;;

		return $uuid;
	}
	
	public function iconvUtil($value,$encode){
		$encode1 = mb_detect_encoding($value, array("ASCII","CP936","GB2312","GBK","UTF-8"));
		if($encode1!=$encode)
			$value = iconv($encode1, $encode, $value);
		return $value;
	}
}